package nz.ac.massey.jbuif.editor.components;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collection;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import nz.ac.massey.jbuif.editor.BeanEditor;
import nz.ac.massey.jbuif.utils.Utils;

/**
 * Class ListEditorPanel
 * @author AMIR H KIOUMARS
 *
 */
public class ListEditorPanel extends BaseComponent implements ActionListener,ListDataListener,ListSelectionListener {

	private static final long serialVersionUID = 1L;
	@SuppressWarnings("unchecked")
	private Collection cList;
	private Object[]   oArray;
	private JList viewingList;
	private String originalType;
	private ClassLoader classLoader;
	private boolean dataVaild = true;
	private boolean arrayType = false;
	private boolean collectionType = false;
	
	
	//http://java.sun.com/docs/books/tutorial/uiswing/components/list.html
	
	/**
	 * ListEditorPanel
	 * @param p bean editor instance
	 * @param name string name
	 * @param displayName string name to be display
	 * @param pev an object
	 */
	@SuppressWarnings("unchecked")
	public ListEditorPanel(BeanEditor p, String name,String displayName,Object pev) {
		super(p, name, displayName, pev);
		
		viewingList = new JList();
		
		if(pev!= null){
			//originalType = pev.getClass().getName();
			//Get the Array
			if(pev instanceof Collection){
				arrayType = false;
				collectionType = true;
				cList = (Collection)pev;
			} else if (pev instanceof Object[]){
				arrayType = true;
				collectionType = false;
				oArray = (Object[])pev;
				for (Object o : oArray){
					cList.add(o);
				}
			}
			
			viewingList = new JList(cList.toArray());
			viewingList.addListSelectionListener(this);
			viewingList.getModel().addListDataListener(this);
			
			
			JPanel buttonsPanel = new JPanel();
			buttonsPanel.setLayout(new BoxLayout(buttonsPanel, BoxLayout.Y_AXIS));
			JButton addButton = new JButton();
			addButton.setActionCommand("add");
			addButton.setText("Add");
			addButton.addActionListener(this);
			JButton editButton = new JButton();
			editButton.setActionCommand("edit");
			editButton.setText("Edit");
			editButton.addActionListener(this);
			JButton deleteButton = new JButton();
			deleteButton.setActionCommand("delete");
			deleteButton.setText("Delete");
			deleteButton.addActionListener(this);
			
			buttonsPanel.add(addButton);
			buttonsPanel.add(editButton);
			buttonsPanel.add(deleteButton);
			
			super.addPropertyComponent(viewingList);
			super.addPropertyComponent(buttonsPanel);
			dataVaild = true;
		} else {
			dataVaild = false;
		}
	}	
	
	
	/**
	 * setCollectionType
	 * @param ctype collection type name
	 */
	public void setCollectionType(String ctype){
		originalType = ctype;
	}
	
	
	/**
	 * setClassLoader
	 * @param cl ClassLoader instance
	 */ 
	public void setClassLoader(ClassLoader cl){
		classLoader = cl;
	}
	
	
	/**
	 * redisplayList redraw the list
	 */
	private void redisplayList(){
		viewingList.setListData(cList.toArray());
		this.validate();
		this.repaint();
	}
	
	
	/**
	 * actionPerformed
	 * @param e ActionEvent iinstance
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		
		if(!dataVaild)
			return;
		
		if(e.getActionCommand().equals("add")){
			if(super.getBeanEditor().getBeansAvailable().contains(this.originalType)){
				
				Object beanInst = Utils.createBeanFromName(this.originalType, classLoader);	
				super.getBeanEditor().generateEditorForBean(beanInst,true);
				//add to list
			} 
			
		} else if (e.getActionCommand().equals("delete")){
			
			if(this.viewingList.getSelectedIndex()>-1){
				cList.remove(this.viewingList.getSelectedValue());
				redisplayList();
			}
		}
		
		ActionEvent e1 = new ActionEvent(e.getSource(), e.getID(), "listChanged");
				
		//Set to correct type and return/set base property
		if(arrayType){
			super.setPropertyValue(cList.toArray());
		} else if (collectionType){
			super.setPropertyValue(cList);
		}
		
		e1.setSource(this);
		super.getBeanEditor().actionPerformed(e1);
		
	}


	/**
	 * valueChanged controls the value change property
	 * @param arg0 ListSelectionEvent
	 */
	@Override
	public void valueChanged(ListSelectionEvent arg0) {	}


	/**
	 * contentsChanged controls the contents changed property
	 * @param arg0 ListDataEvent
	 */
	@Override
	public void contentsChanged(ListDataEvent arg0) { }


	/**
	 * intervalAdded controls the interval added property
	 * @param arg0 ListDataEvent instance
	 */
	@Override
	public void intervalAdded(ListDataEvent arg0) { }


	/**
	 * intervalRemoved controls the interval removed property 
	 * @param arg0 ListDataEvent instance
	 */
	@Override
	public void intervalRemoved(ListDataEvent arg0) {}
	
}
